package com.hairline.controller;

import com.hairline.aspect.UserFootprintLog;
import com.hairline.bean.Category;
import com.hairline.bean.Goods;
import com.hairline.bean.User;
import com.hairline.bean.vo.*;
import com.hairline.service.WxGoodsService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("wx/goods")
public class WxGoodsController {

    @Autowired
    WxGoodsService wxGoodsService;

    @RequestMapping("list")
    //todo
    public BaseRespVo goodsList(Integer categoryId, Integer page, Integer size) {
        //先用categoryId 查到该类别的sort_id，然后再根据sort_id来查询在这个类别下的商品（再加一个限定条件是level是L2）
        int sortOrder = wxGoodsService.selectSortIdById(categoryId);
        List<Goods> goodsList = wxGoodsService.selectGoodsByCategoryId(categoryId);
        List<Category> filterCategoryList = wxGoodsService.selectGoodsBySortId(sortOrder,page,size);
        HashMap<String, Object> map = new HashMap<>();
        map.put("goodsList",goodsList);
        map.put("count",goodsList.size());
        map.put("filterCategoryList",filterCategoryList);
        return BaseRespVo.ok(map);
    }

    @RequestMapping("detail")
    @UserFootprintLog
    public BaseRespVo goodsDetail(Integer id) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipals().getPrimaryPrincipal();
        WxGoodsDetailVO wxGoodsDetailVO = wxGoodsService.goodsDetail(id, user);
        return BaseRespVo.ok(wxGoodsDetailVO);
    }

    @RequestMapping("related")
    public BaseRespVo goodsRelated(Integer id) {
        WxGoodsRelatedVO wxGoodsRelatedVO = wxGoodsService.goodsRelated(id);
        return BaseRespVo.ok(wxGoodsRelatedVO);
    }
}
